package com.group1.ws;

import java.util.Collection;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;

import com.group1.model.Lecturer;
import com.group1.model.Subject;
import com.group1.model.Topic;
import com.group1.util.SessionFactoryUtil;

@Path("/topic")
public class TopicWS {

	@GET
	@Path("/getAll")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAllTopic()
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Topic> list =  Topic.getAllTopic(session);
		String result;
		result = "{TOPIC:[";
		boolean first = true;
		for (Topic t : list) {
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicID\"" + ":\"" + t.getTopicId() + "\",";
			result += "\"topicName\"" + ":\"" + t.getTopicName() + "\",";
			if(t.getTOPIC_PARENT_ID()!=null)
			{
				result += "\"topTopicID\"" + ":\"" + t.getTOPIC_PARENT_ID().getTopicName()+ "\",";
			}
			else
			{
				result += "\"topTopicID\"" + ":\""+ "\",";
			}
				
			result += "\"topicNote\"" + ":\"" + t.getTopicNote()+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	@GET
	@Path("/getByID/{topicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getTopicByID(@PathParam("topicID") String topicID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Topic t = Topic.getTopicByID(Integer.valueOf(topicID), session);
		String result;
		result = "{TOPIC:";
		result += "{";
		result += "\"topicID\"" + ":\"" + t.getTopicId() + "\",";
		result += "\"topicName\"" + ":\"" + t.getTopicName() + "\",";
		if(t.getTOPIC_PARENT_ID()!=null)
		{
			result += "\"topTopicID\"" + ":\"" + t.getTOPIC_PARENT_ID().getTopicName()+ "\",";
		}
		else
		{
			result += "\"topTopicID\"" + ":\""+ "\",";
		}
		result += "\"topicNote\"" + ":\"" + t.getTopicNote()+ "\"";
		result += "}}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getBySubject/{subjectID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getTopicBySubject(@PathParam("subjectID") String subjectID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		Collection<Topic> list = Topic.getTopicBySubject(subjectID, session);
		String result;
		result = "{TOPIC:[";
		boolean first = true;
		for (Topic t : list) {
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicID\"" + ":\"" + t.getTopicId() + "\",";
			result += "\"topicName\"" + ":\"" + t.getTopicName() + "\",";
			if(t.getTOPIC_PARENT_ID()!=null)
			{
				result += "\"topTopicID\"" + ":\"" + t.getTOPIC_PARENT_ID().getTopicName()+ "\",";
			}
			else
			{
				result += "\"topTopicID\"" + ":\""+ "\",";
			}
			result += "\"topicNote\"" + ":\"" + t.getTopicNote()+ "\"";
			result += "}";
			
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	
	@GET
	@Path("/getByTopTopic/{topTopicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getTopicByTopTopic(@PathParam("topTopicID") String topTopicID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		Collection<Topic> list = Topic.getTopicByTopTopic(Integer.valueOf(topTopicID), session);
		String result;
		result = "{TOPIC:[";
		boolean first = true;
		for (Topic t : list) {
			result += (first?"":",");
			first = false;
			result += "{";
			result += "\"topicID\"" + ":\"" + t.getTopicId() + "\",";
			result += "\"topicName\"" + ":\"" + t.getTopicName() + "\",";
			if(t.getTOPIC_PARENT_ID()!=null)
			{
				result += "\"topTopicID\"" + ":\"" + t.getTOPIC_PARENT_ID().getTopicName()+ "\",";
			}
			else
			{
				result += "\"topTopicID\"" + ":\""+ "\",";
			}
			result += "\"topicNote\"" + ":\"" + t.getTopicNote()+ "\"";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	
	@GET
	@Path("/create/{topicName}/{topTopicID}/{topicNote}/{subjectID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String createTopic( @PathParam("topicName") String topicName, 
			@PathParam("topTopicID") String topTopicID, @PathParam("topicNote") String topicNote,
			@PathParam("subjectID") String subjectID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			   	Topic t = new Topic();
			   	t.setTopicName(topicName);
			   	if(!topTopicID.equals("0"))
			   	{
			   			t.setTOPIC_PARENT_ID(Topic.getTopicByID(Integer.valueOf(topTopicID), session));
			   	}
			   	t.setTopicNote(topicNote);
			   	t.getSubjectTopicMapping().add(Subject.getSubjectById(subjectID, session));
			   	session.save(t);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/update/{topicID}/{topicName}/{topTopicID}/{topicNote}")
	@Produces(MediaType.TEXT_PLAIN)
	public String updateTopic(@PathParam("topicID") String topicID, @PathParam("topicName") String topicName, 
			@PathParam("topTopicID") String topTopicID, @PathParam("topicNote") String topicNote)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	Topic t = Topic.getTopicByID(Integer.valueOf(topicID), session);
			 	t.setTopicName(topicName);
			 	t.setTopicNote(topicNote);
			 	if(!topTopicID.equals("0"))
			   	{
			 		if(!topTopicID.equals(topicID))
			 			t.setTOPIC_PARENT_ID(Topic.getTopicByID(Integer.valueOf(topTopicID), session));
			   	}
				session.saveOrUpdate(t);
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 session.getTransaction().commit();
		 return "success";
	}
	
	@GET
	@Path("/delete/{topicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteSubject(@PathParam("topicID") String topicID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 session.delete(Topic.getTopicByID(Integer.valueOf(topicID), session));
			 session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			session.clear();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
}
